<template>
  <div class="home-container">
    <!-- 导航栏 -->
    <van-nav-bar class="page-nav-bar" fixed>
      <van-button
        class="search-btn"
        slot="title"
        type="info"
        size="small"
        round
        icon="search"
        to="/search"
        >搜索</van-button
      >
    </van-nav-bar>
    <!-- /导航栏 -->
    <!-- 频道tab栏 -->
    <van-tabs v-model="active" class="channel-tabs" animated swipeable>
      <van-tab :title="obj.name" v-for="obj in channels" :key="obj.id">
        <!-- //频道的文章列表 -->
        <ArticleList :channel="obj"></ArticleList>
        <!-- //频道的文章列表/ -->
      </van-tab>
      <div slot="nav-right" class="placholder"></div>
      <div slot="nav-right" class="hamburger-btn">
        <i class="toutiao toutiao-gengduo" @click="showPopup"></i>
      </div>
    </van-tabs>
    <!-- /频道tab栏 -->
    <!-- 弹出层 -->
    <van-popup
      closeable
      close-icon-position="top-left"
      v-model="show"
      position="bottom"
      :style="{ height: '100%' }"
    >
      <ChannelEdit
        :channels="channels"
        :active="active"
        @update-active="onUpdateActive"
      ></ChannelEdit>
    </van-popup>
  </div>
</template>

<script>
import { getUserChannels } from '@/api/user';
import ChannelEdit from '../../components/channel-edit.vue';
import ArticleList from '../../components/article-list.vue';
import { mapState } from 'vuex';
import { getItem } from '../../utils/storage';
export default {
  name: 'HomePage',
  components: { ArticleList, ChannelEdit },
  props: {},
  data () {
    return {
      active: 0,
      channels: [],
      show: false
    };
  },
  computed: {
    ...mapState(['user'])
  },
  watch: {},
  created () {
    this.loadUserChannels();
  },
  mounted () {},
  methods: {
    async loadUserChannels () {
      // const {
      //   data: {
      //     data: { channels },
      //   },
      // } = await getUserChannels();
      // this.channels = channels;
      let channels = [];
      if (this.user) {
        const { data } = await getUserChannels();
        channels = data.data.channels;
      } else {
        const localChannels = getItem('TOUTIAO_CHANNELS');
        if (localChannels) {
          channels = localChannels;
        } else {
          const { data } = await getUserChannels();
          channels = data.data.channels;
        }
      }
      this.channels = channels;
    },
    showPopup () {
      this.show = true;
    },
    onUpdateActive (index, show = true) {
      this.active = index;
      this.show = show;
    }
  }
};
</script>

<style scoped lang="less">
.home-container {
  padding-bottom: 100px;
  padding-top: 174px;
  // 原因：当 style 标签里有scoped属性时，在scoped作用域组件样式中，默认只能在作用在组件的根节点，如果需要影响其后代元素，需要使用深度选择器
  /deep/ .van-nav-bar__title {
    max-width: unset;
  }
  .search-btn {
    width: 555px;
    height: 64px;
    background-color: #5babfb;
    border: none;
    font-size: 28px;
    .van-icon {
      font-size: 32px;
    }
  }
  /deep/ .channel-tabs {
    .van-tabs__wrap {
      height: 82px;
    }
    .van-tab {
      min-width: 200px;
      border-right: 1px solid #edeff3;
      font-size: 30px;
      color: #777777;
    }
    .van-tab--active {
      color: #333;
    }
    .van-tabs__line {
      width: 31px !important;
      height: 6px;
      background-color: #2992ff;
      bottom: 8px;
    }
    .van-tabs__nav {
      padding-bottom: 0;
    }
    .placholder {
      width: 66px;
      height: 82px;
      flex-shrink: 0;
    }
    .hamburger-btn {
      position: fixed;
      display: flex;
      justify-content: center;
      align-items: center;
      width: 66px;
      height: 82px;
      right: 0;
      background-color: #fff;
      opacity: 0.97;
      i.toutiao {
        font-size: 33px;
      }
      &::before {
        content: "";
        position: absolute;
        left: 0;
        width: 1px;
        height: 100%;
        background-image: url("../../assets/gradient-gray-line.png");
        background-size: contain;
      }
    }
  }
}
</style>
